﻿using UnityEngine;
using System.Collections;

public class CharactorController : MonoBehaviour {

	public ShootController gunMain;
	public int shootFrequency = 10;
	public float velocity = 60;
	public float speed = 0.01F;  

	private float elaspedTimeSinceLastShoot;
	// Use this for initialization
	void Start () {
		elaspedTimeSinceLastShoot = 0;
	}
	
	// Update is called once per frame
	void Update () {
//		//update key events
		updateKeys ();

//		//update touch events
//		updateTouchEvents ();

		elaspedTimeSinceLastShoot += Time.deltaTime;
		if(elaspedTimeSinceLastShoot > 1.0/shootFrequency){
			gunMain.fire();
			elaspedTimeSinceLastShoot = 0;
		}
	}

	void updateKeys(){
		Vector3 moveDirection = new Vector3 (0, velocity/100.0f*Time.deltaTime, 0);
		float moveStep = 0.1f;
		if (Input.GetKey(KeyCode.W)) {
			moveDirection.y += moveStep;
		}
		if (Input.GetKey(KeyCode.S)) {
			moveDirection.y -= moveStep;
		}
		if (Input.GetKey(KeyCode.A)) {
			moveDirection.x -= moveStep;
		}
		if (Input.GetKey(KeyCode.D)) {
			moveDirection.x += moveStep;
		}
		gameObject.transform.Translate (moveDirection);
		Camera.main.transform.Translate (new Vector3 (0, velocity/100.0f*Time.deltaTime, 0));
	}

	void updateTouchEvents(){
		if (Input.touchCount == 1)
		{
			Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
			Vector2 touchPos = new Vector2(wp.x, wp.y);
			
			if (collider2D.OverlapPoint(touchPos))
			{
				Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;  
				transform.position = new Vector3(touchPos.x,touchPos.y,0);
			}
		}
	}
}
